package com.xqh.base.util.word;

import com.xqh.base.pool.ThreadPoolUtils;
import com.xqh.base.util.encry.EncryUtils;
import com.xqh.base.util.excel.ExcelReader;
import com.xqh.base.util.http.HttpClientUtils;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 有道词典
 * 下载音频 , 单词 , 翻译等信息
 */
public class YouDaoTranUtils {

    private static final String DOWNLOAD_AUDIO_URL ="https://dict.youdao.com/dictvoice?type=2&audio=";

    private static final String TRANSLATE_URL = "https://openapi.youdao.com/api";

    private static final String appKey="0912400bb7976544";

    private static final String salt="123456";

    private static final String from = "EN";

    private static final String to="zh-CHS";

    private static final String secretKey="6IUDm010WlFl1SLXBJOPt85uB8Pppbxj";


    /**
     * 开启下载任务
     * @param filePath
     * @param savePath
     * @param startRow 开始行
     * @param sheetIndex
     * @param colIndex 列序号
     */
    public static void startTask(String filePath, String savePath, int sheetIndex, int startRow, int colIndex) throws UnsupportedEncodingException {
        List<String[]> list = ExcelReader.getExcelData(filePath,startRow, sheetIndex);
        for(String[] array:list){
            if(array.length>colIndex && StringUtils.hasText(array[colIndex])){
                String word = array[colIndex];
                String searchWord=word;
                if(word.contains(" ")){
                    searchWord = URLEncoder.encode(word, "UTF-8");
                }
                DownLoadThread t = new DownLoadThread(DOWNLOAD_AUDIO_URL +searchWord, savePath, word.concat(".mp3"));
                ThreadPoolUtils.execute(t);
            }
        }
    }

    /**
     *
     * 下载线程
     */
    public static class DownLoadThread implements Runnable{

        private String url;

        private String saveDir;

        private String fileName;

        public DownLoadThread(String url, String saveDir, String fileName){
            this.url=url;
            this.saveDir=saveDir;
            this.fileName=fileName;
        }

        @Override
        public void run() {
            HttpClientUtils.downloadFile2(url, saveDir, fileName);
        }
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
//        startTask("D:\\九上词汇预习unit7.xls", "D:\\audios\\unit7", 1, 1, 1);
        startTask("D:\\ssat单词跟读7-8组.xls", "D:\\audios\\7-8", 1, 1, 1);
//        getWordInfo("Friends are very important in people's lives");
    }

    public static void getWordInfo(String word){
        Map<String,String> map = new HashMap<>();
        map.put("q", word);
        map.put("from", from);
        map.put("to", to);
        map.put("appKey", appKey);
        map.put("salt",salt);
        map.put("sign", EncryUtils.getMd5(appKey+word+salt+secretKey));
        String response = HttpClientUtils.post(TRANSLATE_URL, map);
        /**
         * 返回结果:
         * {"tSpeakUrl":"http://openapi.youdao.com/ttsapi?q=%E6%9A%82%E5%81%9C&langType=zh-CHS&sign=073243BA831BAF787EA5984688A59187&salt=1523503013245&voice=4&format=mp3&appKey=0912400bb7976544","web":[{"value":["暂停","挂起","禁赛"],"key":"Suspend"},{"value":["停止支付","暂停付款","止付"],"key":"suspend payment"},{"value":["挂起类型","睡眠模式","设置挂起的类型"],"key":"Suspend Type"}],"query":"suspend","translation":["暂停"],"errorCode":"0","dict":{"url":"yddict://m.youdao.com/dict?le=eng&q=suspend"},"webdict":{"url":"http://m.youdao.com/dict?le=eng&q=suspend"},"basic":{"us-phonetic":"sə'spɛnd","phonetic":"sə'spend","uk-phonetic":"sə'spend","uk-speech":"http://openapi.youdao.com/ttsapi?q=suspend&langType=en&sign=D5E44B053D453D5CCB7078CD71ED2C10&salt=1523503013245&voice=4&format=mp3&appKey=0912400bb7976544","explains":["vt. 延缓，推迟；使暂停；使悬浮","vi. 悬浮；禁赛"],"us-speech":"http://openapi.youdao.com/ttsapi?q=suspend&langType=en&sign=D5E44B053D453D5CCB7078CD71ED2C10&salt=1523503013245&voice=0&format=mp3&appKey=0912400bb7976544"},"l":"EN2zh-CHS","speakUrl":"http://openapi.youdao.com/ttsapi?q=suspend&langType=en&sign=D5E44B053D453D5CCB7078CD71ED2C10&salt=1523503013245&voice=4&format=mp3&appKey=0912400bb7976544"}

         */
        System.out.println(response);
    }

}
